#!/bin/bash
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.
#
# This script is meant for running a small server in production. It starts the Diaspora
# server, redis, and resque. To run a server locally for development, use foreman instead:
#    foreman start
#

# ensure right directory
realpath=$( ruby -e "puts File.expand_path(\"$0\")")
cd $(dirname $realpath)/..

# Check if script_server.yml exists
if [ ! -e 'config/script_server.yml' ]; then
    echo "INFO: config/script_server_config.yml has been renamed to config/script_server.yml" >&2
    echo 'FATAL: config/script_server.yml is missing! Copy over config/script_server.yml.example to config/script_server.yml and edit it properly!' >&2
    exit 69
fi


# Check if database.yml exists
if [ ! -e 'config/database.yml' ]; then
    echo 'FATAL: config/database.yml is missing! Copy over config/database.yml.example to config/database.yml and edit it properly!' >&2
    exit 68
fi


# Check if application.yml exists
if [ ! -e 'config/application.yml' ]; then
    echo 'FATAL: config/application.yml is missing! Copy over config/application.yml.example to config/application.yml and edit it properly!' >&2
    exit 70
fi




# Setup environment
OS=`uname -s`
export RAILS_ENV=$(bundle exec ruby ./script/get_config.rb rails_env script_server)
export DB=$(bundle exec ruby ./script/get_config.rb db script_server)
THIN_PORT=$(bundle exec ruby ./script/get_config.rb thin_port script_server)
eval "DEFAULT_THIN_ARGS=\"$(bundle exec ruby ./script/get_config.rb default_thin_args script_server)\""

# Backward compatibillity, overide default settings
[ -e config/server.sh ] && source config/server.sh


function chk_service
{
    port=${1:?Missing port}
    case $OS in
    *[Bb][Ss][Dd]*|Darwin)
        ## checks ipv[46]
        netstat -anL | awk '{print $2}' | grep "\.$1$"
    ;;
    *)
        # Is someone listening on the ports already? (ipv4 only test ?)
        netstat -nl | grep '[^:]:'$port'[ \t]'
    ;;
    esac
}


# Scan for -p, find out what port thin is about to use.
args="$DEFAULT_THIN_ARGS $@"
prev_arg=''
for arg in $( echo $args | awk '{ for (i = 1; i <= NF; i++) print $i}')
do
    [ "$prev_arg" = '-p' ] && THIN_PORT="$arg"
    prev_arg="$arg"
done


# Is someone listening on the ports already? (ipv4 only test ?)
services=$( chk_service $THIN_PORT )
if [ -n "$services" ]; then
    echo "FATAL: Error: thin port $THIN_PORT is already in use. Exiting" >&2
    echo "     $services"
    exit 64
fi

# Force AGPL
if [ -w public -a ! -e  public/source.tar.gz ]; then
    branch=$( git branch | awk '/^[*]/ {print $2}')
    tar czf public/source.tar.gz  `git ls-tree -r $branch | awk '{print $4}'`
fi
if [ ! -e public/source.tar.gz ]; then
    echo "FATAL: Error: Can't find, or even create, public/source.tar.gz. Exiting" >&2
    exit 65
fi


# Jammit notice
if [ ! -e 'public/assets/default.css' ]; then
    if [ "$RAILS_ENV" == 'production' ]; then
        echo "INFO: If you want further performance improvements," >&2
        echo "after each git pull before you restart the application, run:" >&2
        echo "bundle exec rake assets:precompile" >&2
    fi
fi


# Start Diaspora

if [ "$(bundle exec ruby ./script/get_config.rb 'single_process_mode?')" != "true" ]; then
    QUEUE=* bundle exec rake resque:work&
fi

if [ "$(./script/get_config.rb enable_thin script_server)" = "true" ]; then
    bundle exec thin start $args
fi
